//
//  VisitorLoginView.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/1.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit
////处理登录和注册的事件
//protocol VisitorLoginViewDelegate:NSObjectProtocol{
//    func visitorLoginViewWillRegister();
//    func visitorLoginViewWillLogin();
//}
class VisitorLoginView: UIView {

//    weak var delegate:VisitorLoginViewDelegate?
//    @objc private func clickRegister(){
//        delegate?.visitorLoginViewWillRegister()
//    }
//    @objc private func clickLogiin(){
//        delegate?.visitorLoginViewWillLogin()
//    }
    
    func setupInfo(imageName:String?,message:String){
        messageLabel.text = message
        if let imgName = imageName {
            iconView.image = UIImage(named: imgName)
            homeIconView.hidden = true
            //将遮罩层移动到底部
            sendSubviewToBack(maskIconView)
            printLog("移除动画")
            iconView.layer.removeAllAnimations()//移除动画
        }else{
            startAnimation()
        }
    }
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*M_PI
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    /**
    *  重写init设置视图控件 纯代码和xib两种写法
    */
    override init(frame: CGRect) {
        super.init(frame:frame);
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        setupUI()
    }
    //定义图标控件
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
        }()
    //定义小房子控件
    private lazy var homeIconView:UIImageView = {
        let hv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return hv
        }()
    //定义描述文字的Lable
    private lazy var messageLabel:UILabel = {
        let ml = UILabel()
        ml.text = "关注一些人，回这里看看有什么惊喜"
        ml.textColor = UIColor.darkGrayColor()
        ml.font = UIFont.systemFontOfSize(14)
        ml.numberOfLines = 0
        ml.textAlignment = NSTextAlignment.Center
       // ml.sizeToFit()
        return ml
        }()
    //注册按钮
    lazy var registerButton:UIButton = {
        let rb = UIButton()
        rb.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        rb.setTitle("注册", forState: UIControlState.Normal)
        rb.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        return rb
        }()
    //登录按钮
    lazy var loginButton:UIButton = {
        let lb = UIButton()
        lb.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        lb.setTitle("登录", forState: UIControlState.Normal)
        lb.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        return lb
        }()
    //遮罩层
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //添加其余控件
    private func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //取消自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        //添加约束
        //图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //展示文本
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        //registerButton.bounds = CGRect(x: 0, y: 0, width: 100, height: 35)
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        //registerButton.bounds = CGRect(x: 0, y: 0, width: 100, height: 35)
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //遮罩层
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        //options 两个控件的约束类型
        let cons = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": maskIconView])
        addConstraints(cons)
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(220)-|", options: [], metrics: nil, views: ["v":maskIconView,"regButton":registerButton]))//-35
    }
}
